package cn.dataling.ai.controller;

import cn.dataling.ai.common.Response;
import cn.dataling.ai.component.XcxComponent;
import cn.dataling.ai.domain.AiChatConversation;
import cn.dataling.ai.domain.AiChatMemory;
import cn.dataling.ai.mapper.AiChatConversationMapper;
import cn.dataling.ai.mapper.AiChatMemoryMapper;
import cn.dataling.ai.model.XcxLoginDTO;
import cn.dataling.ai.model.XcxLoginVO;
import cn.dataling.ai.util.LocalCache;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import cn.dataling.ai.component.WebSocketServer;
import jakarta.annotation.Resource;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.time.LocalDateTime;
import java.util.List;


@RestController
@RequestMapping("/chat")
public class ChatController {

    @Resource
    private XcxComponent xcxComponent;
    @Resource
    private AiChatMemoryMapper aiChatMemoryMapper;
    @Resource
    private AiChatConversationMapper aiChatConversationMapper;

    @GetMapping("/chat")
    public Response<Boolean> chat(@RequestParam(value = "openId") String openId,
                                  @RequestParam(value = "message") String message,
                                  @RequestParam(value = "conversationId") String conversationId) {

        LocalCache.get(conversationId, Boolean.class, isExist -> {
            if (ObjectUtils.isEmpty(isExist)) {

                boolean exists = aiChatConversationMapper.exists(new LambdaQueryWrapper<AiChatConversation>()
                        .eq(AiChatConversation::getConversationId, conversationId));
                if (!exists){
                    AiChatConversation aiChatConversation = new AiChatConversation();
                    aiChatConversation.setConversationTitle(message);
                    aiChatConversation.setConversationId(conversationId);
                    aiChatConversation.setCreatedAt(LocalDateTime.now());
                    aiChatConversation.setOpenId(openId);

                    aiChatConversationMapper.insert(aiChatConversation);
                }

                LocalCache.set(conversationId, true);
            }
        });

        WebSocketServer.sendMessage(message, openId, conversationId);
        return Response.success(true);
    }

    @GetMapping("/stopAnswer")
    public Response<Boolean> stopAnswer(@RequestParam(value = "openId") String openId) {
        return Response.success(WebSocketServer.stopAnswer(openId));
    }

    @PostMapping("/login")
    public Response<XcxLoginVO> login(@RequestBody @Valid XcxLoginDTO data) {
        return Response.success(xcxComponent.xcxLogin(data));
    }

    @GetMapping("/getChatConversation")
    public Response<List<AiChatConversation>> getChatConversation(@RequestParam(value = "openId") String openId) {

        List<AiChatConversation> aiChatMemories = aiChatConversationMapper.selectList(new LambdaQueryWrapper<AiChatConversation>()
                .eq(AiChatConversation::getOpenId, openId)

        );

        return Response.success(aiChatMemories);
    }

    @GetMapping("/deleteConversation")
    public Response<Integer> deleteConversation(@RequestParam(value = "id") String id) {
        return Response.success(aiChatConversationMapper.deleteById(id));
    }

    @GetMapping("/getChatMemory")
    public Response<List<AiChatMemory>> getChatMemory(@RequestParam(value = "conversationId") String conversationId) {

        List<AiChatMemory> aiChatMemories = aiChatMemoryMapper.selectList(new LambdaQueryWrapper<AiChatMemory>()
                .eq(AiChatMemory::getConversationId, conversationId)
        );

        return Response.success(aiChatMemories);
    }
}
